 package action;

import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.Disciplina;
import padraoMemento.MementoDisciplina;
public class EditarDisciplinaAction extends ManterDisciplinaAction {
   
    @Override
    public void setParametros(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        try {
            
            request.setAttribute("editar", "readonly");
            request.setAttribute("excluir", "");
            
            int codDisciplina = Integer.parseInt(request.getParameter("codDisciplina"));  
            Disciplina disciplina = Disciplina.obterDisciplina(codDisciplina);
            request.setAttribute("disciplina", disciplina);
            String preRequisitosSerialized = "";
            for (Disciplina preRequisito : disciplina.getDisciplinasForCodDisciplinaPreRequisito()) {
                preRequisitosSerialized = preRequisitosSerialized + preRequisito.getCodDisciplina() + ",";
            }
            request.setAttribute("preRequisitosSerialized", preRequisitosSerialized);
        } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

    @Override
    public void efetuaPersistencia(Disciplina disciplina) throws ServletException {
      try {
            MementoDisciplina.adicionarDisciplina(Disciplina.obterDisciplina(disciplina.getCodDisciplina()));
            disciplina.editar();
        
      } catch (SQLException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new ServletException(e);
        }
    }

}
